package c.e.b.m;

import android.text.TextUtils;
import android.util.Log;
import c.e.a.b.g.a0;
import c.e.a.b.g.g;
import c.e.a.b.g.h;
import c.e.b.c;
import c.e.b.m.l.b;
import c.e.b.m.m.e;
import c.e.b.o.f;
import com.huawei.agconnect.exception.AGCServerException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

/* compiled from: FirebaseInstallations */
public class d implements e {
    public static final Object l = new Object();
    public static final ThreadFactory m = new a();
    public final c a;
    public final c.e.b.m.m.c b;
    public final c.e.b.m.l.c c;
    public final k d;
    public final b e;
    public final i f;
    public final Object g = new Object();
    public final ExecutorService h;
    public final ExecutorService i;
    public String j;
    public final List<j> k = new ArrayList();

    /* compiled from: FirebaseInstallations */
    public class a implements ThreadFactory {
        public final AtomicInteger a = new AtomicInteger(1);

        public Thread newThread(Runnable runnable) {
            return new Thread(runnable, String.format("firebase-installations-executor-%d", new Object[]{Integer.valueOf(this.a.getAndIncrement())}));
        }
    }

    public d(c cVar, f fVar, c.e.b.k.c cVar2) {
        TimeUnit timeUnit = TimeUnit.SECONDS;
        LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue();
        ThreadFactory threadFactory = m;
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, 1, 30, timeUnit, linkedBlockingQueue, threadFactory);
        cVar.a();
        c.e.b.m.m.c cVar3 = new c.e.b.m.m.c(cVar.a, fVar, cVar2);
        c.e.b.m.l.c cVar4 = new c.e.b.m.l.c(cVar);
        k kVar = new k();
        b bVar = new b(cVar);
        i iVar = new i();
        this.a = cVar;
        this.b = cVar3;
        this.c = cVar4;
        this.d = kVar;
        this.e = bVar;
        this.f = iVar;
        this.h = threadPoolExecutor;
        this.i = new ThreadPoolExecutor(0, 1, 30, timeUnit, new LinkedBlockingQueue(), threadFactory);
    }

    public static d d() {
        c b = c.b();
        b.i.b.f.K(true, "Null is not a valid value of FirebaseApp.");
        b.a();
        return (d) b.d.a(e.class);
    }

    public final c.e.b.m.l.d a(c.e.b.m.l.d dVar) throws f {
        c.e.b.m.f.a aVar = c.e.b.m.f.a.UNAVAILABLE;
        c.e.b.m.m.c cVar = this.b;
        String b = b();
        c.e.b.m.l.a aVar2 = (c.e.b.m.l.a) dVar;
        String str = aVar2.b;
        String e = e();
        String str2 = aVar2.e;
        Objects.requireNonNull(cVar);
        r7 = new Object[2];
        int i = 0;
        r7[0] = e;
        r7[1] = str;
        URL a = cVar.a(String.format("projects/%s/installations/%s/authTokens:generate", r7));
        while (i <= 1) {
            HttpURLConnection c = cVar.c(a, b);
            try {
                e f;
                c.setRequestMethod("POST");
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("FIS_v2 ");
                stringBuilder.append(str2);
                c.addRequestProperty("Authorization", stringBuilder.toString());
                cVar.h(c);
                int responseCode = c.getResponseCode();
                if (responseCode == AGCServerException.OK) {
                    f = cVar.f(c);
                } else {
                    c.e.b.m.m.b.b bVar;
                    c.e.b.m.m.c.b(c, null, b, e);
                    if (responseCode != AGCServerException.TOKEN_INVALID) {
                        if (responseCode != 404) {
                            if (responseCode == 429) {
                                continue;
                            } else if (responseCode < AGCServerException.UNKNOW_EXCEPTION || responseCode >= 600) {
                                Log.e("Firebase-Installations", "Firebase Installations can not communicate with Firebase server APIs due to invalid configuration. Please update your Firebase initialization process and set valid Firebase options (API key, Project ID, Application ID) when initializing Firebase.");
                                bVar = (c.e.b.m.m.b.b) e.a();
                                bVar.c = e.b.BAD_CONFIG;
                                f = bVar.a();
                            }
                            i++;
                            c.disconnect();
                        }
                    }
                    bVar = (c.e.b.m.m.b.b) e.a();
                    bVar.c = e.b.AUTH_ERROR;
                    f = bVar.a();
                }
                c.disconnect();
                c.e.b.m.m.b bVar2 = (c.e.b.m.m.b) f;
                int ordinal = bVar2.c.ordinal();
                c.e.b.m.l.a.b bVar3;
                if (ordinal == 0) {
                    String str3 = bVar2.a;
                    long j = bVar2.b;
                    long a2 = this.d.a();
                    bVar3 = (c.e.b.m.l.a.b) dVar.j();
                    bVar3.c = str3;
                    bVar3.e = Long.valueOf(j);
                    bVar3.f = Long.valueOf(a2);
                    return bVar3.a();
                } else if (ordinal == 1) {
                    bVar3 = (c.e.b.m.l.a.b) dVar.j();
                    bVar3.g = "BAD CONFIG";
                    bVar3.b(c.e.b.m.l.c.a.REGISTER_ERROR);
                    return bVar3.a();
                } else if (ordinal == 2) {
                    synchronized (this) {
                        this.j = null;
                    }
                    c.e.b.m.l.d.a j2 = dVar.j();
                    j2.b(c.e.b.m.l.c.a.NOT_GENERATED);
                    return j2.a();
                } else {
                    throw new f("Firebase Installations Service is unavailable. Please try again later.", aVar);
                }
            } catch (IOException unused) {
            } catch (Throwable th) {
                c.disconnect();
            }
        }
        throw new f("Firebase Installations Service is unavailable. Please try again later.", aVar);
    }

    public String b() {
        c cVar = this.a;
        cVar.a();
        return cVar.c.a;
    }

    public String c() {
        c cVar = this.a;
        cVar.a();
        return cVar.c.b;
    }

    public String e() {
        c cVar = this.a;
        cVar.a();
        return cVar.c.g;
    }

    public final String f(c.e.b.m.l.d dVar) {
        c cVar = this.a;
        cVar.a();
        if (cVar.b.equals("CHIME_ANDROID_SDK") || this.a.g()) {
            if ((((c.e.b.m.l.a) dVar).c == c.e.b.m.l.c.a.ATTEMPT_MIGRATION ? 1 : null) != null) {
                String string;
                b bVar = this.e;
                synchronized (bVar.a) {
                    synchronized (bVar.a) {
                        string = bVar.a.getString("|S|id", null);
                    }
                    if (string != null) {
                    } else {
                        string = bVar.a();
                    }
                }
                if (TextUtils.isEmpty(string)) {
                    string = this.f.a();
                }
                return string;
            }
        }
        return this.f.a();
    }

    /* JADX WARNING: Missing exception handler attribute for start block: B:18:0x0061 */
    /* JADX WARNING: Can't wrap try/catch for region: R(6:64|13|(2:15|16)(1:17)|18|19|20) */
    public final c.e.b.m.l.d g(c.e.b.m.l.d r22) throws c.e.b.m.f {
        /*
        r21 = this;
        r1 = r21;
        r0 = c.e.b.m.f.a.UNAVAILABLE;
        r2 = r22;
        r2 = (c.e.b.m.l.a) r2;
        r3 = r2.b;
        r4 = 0;
        r5 = 0;
        if (r3 == 0) goto L_0x006b;
    L_0x000e:
        r3 = r3.length();
        r6 = 11;
        if (r3 != r6) goto L_0x006b;
    L_0x0016:
        r3 = r1.e;
        r6 = r3.a;
        monitor-enter(r6);
        r7 = c.e.b.m.l.b.c;	 Catch:{ all -> 0x0068 }
        r8 = r7.length;	 Catch:{ all -> 0x0068 }
        r9 = 0;
    L_0x001f:
        if (r9 >= r8) goto L_0x0066;
    L_0x0021:
        r10 = r7[r9];	 Catch:{ all -> 0x0068 }
        r11 = r3.b;	 Catch:{ all -> 0x0068 }
        r12 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0068 }
        r12.<init>();	 Catch:{ all -> 0x0068 }
        r13 = "|T|";
        r12.append(r13);	 Catch:{ all -> 0x0068 }
        r12.append(r11);	 Catch:{ all -> 0x0068 }
        r11 = "|";
        r12.append(r11);	 Catch:{ all -> 0x0068 }
        r12.append(r10);	 Catch:{ all -> 0x0068 }
        r10 = r12.toString();	 Catch:{ all -> 0x0068 }
        r11 = r3.a;	 Catch:{ all -> 0x0068 }
        r10 = r11.getString(r10, r4);	 Catch:{ all -> 0x0068 }
        if (r10 == 0) goto L_0x0063;
    L_0x0046:
        r11 = r10.isEmpty();	 Catch:{ all -> 0x0068 }
        if (r11 != 0) goto L_0x0063;
    L_0x004c:
        r3 = "{";
        r3 = r10.startsWith(r3);	 Catch:{ all -> 0x0068 }
        if (r3 == 0) goto L_0x0060;
    L_0x0054:
        r3 = new org.json.JSONObject;	 Catch:{ JSONException -> 0x0061 }
        r3.<init>(r10);	 Catch:{ JSONException -> 0x0061 }
        r7 = "token";
        r4 = r3.getString(r7);	 Catch:{ JSONException -> 0x0061 }
        goto L_0x0061;
    L_0x0060:
        r4 = r10;
    L_0x0061:
        monitor-exit(r6);	 Catch:{ all -> 0x0068 }
        goto L_0x006b;
    L_0x0063:
        r9 = r9 + 1;
        goto L_0x001f;
    L_0x0066:
        monitor-exit(r6);	 Catch:{ all -> 0x0068 }
        goto L_0x006b;
    L_0x0068:
        r0 = move-exception;
        monitor-exit(r6);	 Catch:{ all -> 0x0068 }
        throw r0;
    L_0x006b:
        r3 = r1.b;
        r6 = r21.b();
        r2 = r2.b;
        r7 = r21.e();
        r8 = r21.c();
        java.util.Objects.requireNonNull(r3);
        r9 = 1;
        r10 = new java.lang.Object[r9];
        r10[r5] = r7;
        r11 = "projects/%s/installations";
        r10 = java.lang.String.format(r11, r10);
        r10 = r3.a(r10);
    L_0x008d:
        if (r5 > r9) goto L_0x014e;
    L_0x008f:
        r11 = r3.c(r10, r6);
        r12 = "POST";
        r11.setRequestMethod(r12);	 Catch:{ IOException -> 0x0147, all -> 0x0142 }
        r11.setDoOutput(r9);	 Catch:{ IOException -> 0x0147, all -> 0x0142 }
        if (r4 == 0) goto L_0x00a2;
    L_0x009d:
        r12 = "x-goog-fis-android-iid-migration-auth";
        r11.addRequestProperty(r12, r4);	 Catch:{ IOException -> 0x0147, all -> 0x0142 }
    L_0x00a2:
        r3.g(r11, r2, r8);	 Catch:{ IOException -> 0x0147, all -> 0x0142 }
        r12 = r11.getResponseCode();	 Catch:{ IOException -> 0x0147, all -> 0x0142 }
        r13 = 200; // 0xc8 float:2.8E-43 double:9.9E-322;
        if (r12 != r13) goto L_0x00b5;
    L_0x00ad:
        r2 = r3.e(r11);	 Catch:{ IOException -> 0x0147, all -> 0x0142 }
        r11.disconnect();
        goto L_0x00e2;
    L_0x00b5:
        c.e.b.m.m.c.b(r11, r8, r6, r7);	 Catch:{ IOException -> 0x0147, all -> 0x0142 }
        r13 = 429; // 0x1ad float:6.01E-43 double:2.12E-321;
        if (r12 == r13) goto L_0x0147;
    L_0x00bc:
        r13 = 500; // 0x1f4 float:7.0E-43 double:2.47E-321;
        if (r12 < r13) goto L_0x00c6;
    L_0x00c0:
        r13 = 600; // 0x258 float:8.41E-43 double:2.964E-321;
        if (r12 >= r13) goto L_0x00c6;
    L_0x00c4:
        goto L_0x0147;
    L_0x00c6:
        r12 = "Firebase-Installations";
        r13 = "Firebase Installations can not communicate with Firebase server APIs due to invalid configuration. Please update your Firebase initialization process and set valid Firebase options (API key, Project ID, Application ID) when initializing Firebase.";
        android.util.Log.e(r12, r13);	 Catch:{ IOException -> 0x0147, all -> 0x0142 }
        r18 = 0;
        r17 = 0;
        r16 = 0;
        r15 = 0;
        r19 = c.e.b.m.m.d.a.BAD_CONFIG;	 Catch:{ IOException -> 0x0147, all -> 0x0142 }
        r12 = new c.e.b.m.m.a;	 Catch:{ IOException -> 0x0147, all -> 0x0142 }
        r20 = 0;
        r14 = r12;
        r14.m6184init(r15, r16, r17, r18, r19, r20);	 Catch:{ IOException -> 0x0147, all -> 0x0142 }
        r11.disconnect();
        r2 = r12;
    L_0x00e2:
        r2 = (c.e.b.m.m.a) r2;
        r3 = r2.e;
        r3 = r3.ordinal();
        if (r3 == 0) goto L_0x010a;
    L_0x00ec:
        if (r3 != r9) goto L_0x0102;
    L_0x00ee:
        r0 = "BAD CONFIG";
        r2 = r22.j();
        r2 = (c.e.b.m.l.a.b) r2;
        r2.g = r0;
        r0 = c.e.b.m.l.c.a.REGISTER_ERROR;
        r2.b(r0);
        r0 = r2.a();
        return r0;
    L_0x0102:
        r2 = new c.e.b.m.f;
        r3 = "Firebase Installations Service is unavailable. Please try again later.";
        r2.m6177init(r3, r0);
        throw r2;
    L_0x010a:
        r0 = r2.b;
        r3 = r2.c;
        r4 = r1.d;
        r4 = r4.a();
        r6 = r2.d;
        r6 = r6.c();
        r2 = r2.d;
        r7 = r2.d();
        r2 = r22.j();
        r2 = (c.e.b.m.l.a.b) r2;
        r2.a = r0;
        r0 = c.e.b.m.l.c.a.REGISTERED;
        r2.b(r0);
        r2.c = r6;
        r2.d = r3;
        r0 = java.lang.Long.valueOf(r7);
        r2.e = r0;
        r0 = java.lang.Long.valueOf(r4);
        r2.f = r0;
        r0 = r2.a();
        return r0;
    L_0x0142:
        r0 = move-exception;
        r11.disconnect();
        throw r0;
    L_0x0147:
        r5 = r5 + 1;
        r11.disconnect();
        goto L_0x008d;
    L_0x014e:
        r2 = new c.e.b.m.f;
        r3 = "Firebase Installations Service is unavailable. Please try again later.";
        r2.m6177init(r3, r0);
        throw r2;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.e.b.m.d.g(c.e.b.m.l.d):c.e.b.m.l.d");
    }

    public g<String> getId() {
        String str = "Please set your Application ID. A valid Firebase App ID is required to communicate with Firebase server APIs: It identifies your application with Firebase.Please refer to https://firebase.google.com/support/privacy/init-options.";
        b.i.b.f.O(c(), str);
        b.i.b.f.O(e(), "Please set your Project ID. A valid Firebase Project ID is required to communicate with Firebase server APIs: It identifies your application with Firebase.Please refer to https://firebase.google.com/support/privacy/init-options.");
        String str2 = "Please set a valid API key. A Firebase API key is required to communicate with Firebase server APIs: It authenticates your project with Google.Please refer to https://firebase.google.com/support/privacy/init-options.";
        b.i.b.f.O(b(), str2);
        String c = c();
        Pattern pattern = k.b;
        b.i.b.f.K(c.contains(":"), str);
        b.i.b.f.K(k.b.matcher(b()).matches(), str2);
        synchronized (this) {
            c = this.j;
        }
        if (c != null) {
            return c.e.a.b.b.j.d.b(c);
        }
        h hVar = new h();
        h hVar2 = new h(hVar);
        synchronized (this.g) {
            this.k.add(hVar2);
        }
        a0 a0Var = hVar.a;
        this.h.execute(new b(this));
        return a0Var;
    }

    public final void h(c.e.b.m.l.d dVar, Exception exception) {
        synchronized (this.g) {
            Iterator it = this.k.iterator();
            while (it.hasNext()) {
                if (((j) it.next()).a(dVar, exception)) {
                    it.remove();
                }
            }
        }
    }

    public final void i(c.e.b.m.l.d dVar) {
        synchronized (this.g) {
            Iterator it = this.k.iterator();
            while (it.hasNext()) {
                if (((j) it.next()).b(dVar)) {
                    it.remove();
                }
            }
        }
    }
}
